package com.auto.learning.view;

import java.net.UnknownHostException;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.os.Handler.Callback;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

import com.auto.learning.R;
import com.auto.learning.bean.ShareModelBean;
import com.auto.learning.sharesdk.tools.ShareCommInfo;
import com.auto.learning.sharesdk.tools.ShareOpUtils;

public class ShareView extends PopupWindow implements OnClickListener,PlatformActionListener {
	
	private LinearLayout llWeixin;
	private LinearLayout llQQ;
	private LinearLayout llSinaWeibo;
	private LinearLayout llQQZone;
	private View mMenuView;

	private String title = "";
	private String id = "";
	private String url = "";
	private String content = "";
	private String imageUrl = "";
	private int type = 0;  //0 时不做任何处理     1书  2分享名人堂,3分享读书记录,4分享评论,5分享主播信息, 6 H5页面分享,7书评的评论，8分享书单
	
	private Context context;
	private Callback callback;
	
	private int shareType = -1;
	
	

	public ShareView(Activity context,Callback callback) {
		super(context);
		this.context = context;
		this.callback = callback;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.menu_share, null);

		llWeixin = (LinearLayout) mMenuView.findViewById(R.id.llWeixin);
		llWeixin.setOnClickListener(this);
		llQQ = (LinearLayout) mMenuView.findViewById(R.id.weixin_friend);
		llQQ.setOnClickListener(this);
		llSinaWeibo = (LinearLayout) mMenuView.findViewById(R.id.llSinaWeibo);
		llSinaWeibo.setOnClickListener(this);
		llQQZone = (LinearLayout) mMenuView.findViewById(R.id.qq_zone);
		llQQZone.setOnClickListener(this);

		((TextView) mMenuView.findViewById(R.id.tvCancel)).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);

		ColorDrawable dw = new ColorDrawable(0xb0000000);
		this.setBackgroundDrawable(dw);
	}

	public void show(View view) {
		this.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.llWeixin:
			shareType = 2;
			ShareOpUtils.doShareWeixin(context, callback, ShareCommInfo.WEIXIN_CHAT, this, title, url, content,imageUrl,type);
			dismiss();
			break;
		case R.id.weixin_friend:
			shareType = 3;
			ShareOpUtils.doShareWeixin(context, callback, ShareCommInfo.WEIXIN_FRIEND, this, title, url, content,imageUrl,type);
			dismiss();
			break;
		case R.id.llSinaWeibo:
			shareType = 1;
			ShareOpUtils.doShareSina(context, callback,this, title, url, content,imageUrl);
			dismiss();
			break;
		case R.id.qq_zone:
			shareType = 4;
			ShareOpUtils.doShareQQ(context, callback,this, title, url, content,imageUrl);
			dismiss();
			break;
		}
	}
	

	public ShareView setTitle(String title) {
		this.title = title;
		return this;
	}


	public ShareView setId(String id) {
		this.id = id;
		return this;
	}
	

	public ShareView setUrl(String url) {
		this.url = url;
		return this;
	}
	

	public ShareView setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
		return this;
	}


	public ShareView setContent(String content) {
		this.content = content;
		return this;
	}
	

	public ShareView setType(int type) {
		this.type = type;
		return this;
	}

	@Override
	public void onCancel(Platform platform, int arg1) {
		UIHandler.sendEmptyMessage(ShareCommInfo.SHARE_CANCLE, callback);
	}

	@Override
	public void onComplete(Platform platform, int arg1, HashMap<String, Object> arg2) {
		
//		UIHandler.sendEmptyMessage(ShareCommInfo.SHARE_SUCCESS, callback);
		
		ShareModelBean shareModelBean = new ShareModelBean();
		shareModelBean.setShareType(shareType);
		int x = 0;
		if (type == 1) {
			x = 3;
		}else if (type == 2) {
			x = 2;
		}else if (type == 3) {
			x = 3;
		}else if (type == 4) {
			x = 4;
		}else if (type == 5) {
			x = 5;
		}else if (type == 6) {
			x = 6;
		}else if (type == 7) {
			x = 7;
		}else if (type == 8){
			x = 8;
		}
		shareModelBean.setShareTag(x == 0 ? 1 : x);
		shareModelBean.setShareId(TextUtils.isEmpty(id) ? 0 : Integer.parseInt(id));
		Message message = new Message();
		message.what = ShareCommInfo.SHARE_SUCCESS;
		message.obj = shareModelBean;
		UIHandler.sendMessage(message, callback);
	}

	@Override
	public void onError(Platform platform, int arg1, Throwable arg2){
		if (arg1 == 9 && platform != null && (platform.getName().equals("WechatMoments") || platform.getName().equals("Wechat"))) {
			if (arg2 instanceof WechatClientNotExistException) {
				Message message =new Message();
				message.what = ShareCommInfo.SHARE_FAIL_NO_CLENT;
				message.arg1 = arg1;
				UIHandler.sendMessage(message, callback);
			}else if (arg2 instanceof UnknownHostException) {
				Message message =new Message();
				message.what = ShareCommInfo.SHARE_FAIL_SEND_ERROR;
				message.arg1 = arg1;
				UIHandler.sendMessage(message, callback);
			}
		}
	}
}
